<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备厂家" prop="manufacturers">
        <el-input
          v-model="queryParams.manufacturers"
          placeholder="请输入设备厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所处位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入所处位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="principal">
        <el-input
          v-model="queryParams.principal"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测检验时间">
        <el-date-picker
          v-model="daterangeExamDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="下次检测时间" prop="nextexamDate">
          <el-date-picker clearable
            v-model="queryParams.nextexamDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下次检测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日常检查" prop="dailyExam">
          <el-input
            v-model="queryParams.dailyExam"
            placeholder="请输入日常检查"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="使用单位" prop="useBranch">
        <el-input
          v-model="queryParams.useBranch"
          placeholder="请输入使用单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="创建时间">
          <el-date-picker
            v-model="daterangeCreateTime"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item> -->

      <el-form-item label="购买日期" prop="buyDate">
        <el-date-picker
          clearable
          v-model="queryParams.buyDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购买日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:manager:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:manager:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:manager:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:manager:export']"
          >导出</el-button>
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="managerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="设备型号" align="center" prop="model" />
      <el-table-column label="设备厂家" align="center" prop="manufacturers" />
      <el-table-column label="所处位置" align="center" prop="location" />
      <el-table-column label="技术参数" align="center" prop="argument" />
      <el-table-column
        label="设备图片"
        align="center"
        prop="deviceImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.deviceImage"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="principal" />
      <el-table-column
        label="检测检验时间"
        align="center"
        prop="examDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="下次检测时间"
        align="center"
        prop="nextexamDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextexamDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日常检查" align="center" prop="dailyExam" />
      <el-table-column label="使用单位" align="center" prop="useBranch" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->

      <el-table-column
        label="购买日期"
        align="center"
        prop="buyDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:manager:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:manager:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="height: 60vh; overflow: auto"
      >
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备厂家" prop="manufacturers">
          <el-input v-model="form.manufacturers" placeholder="请输入设备厂家" />
        </el-form-item>
        <el-form-item label="所处位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入所处位置" />
        </el-form-item>
        <el-form-item label="技术参数" prop="argument">
          <el-input
            v-model="form.argument"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="设备图片" prop="deviceImage">
          <image-upload v-model="form.deviceImage" />
        </el-form-item>
        <el-form-item label="责任人" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入管理责任人" />
        </el-form-item>
        <el-form-item label="开始时间" prop="examDate">
          <el-date-picker
            clearable
            v-model="form.examDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择检验时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="nextexamDate">
          <el-date-picker
            clearable
            v-model="form.nextexamDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下次结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日常检查" prop="dailyExam">
          <el-input v-model="form.dailyExam" placeholder="请输入日常检查" />
        </el-form-item>
        <el-form-item label="使用单位" prop="useBranch">
          <el-input v-model="form.useBranch" placeholder="请输入使用单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="矿山系统设备ID：关联原来的设备表的ID" prop="minedeviceId">
            <el-input v-model="form.minedeviceId" placeholder="请输入矿山系统设备ID：关联原来的设备表的ID" />
          </el-form-item> -->
        <!-- <el-form-item label="设备分类ID" prop="cateId">
            <el-input v-model="form.cateId" placeholder="请输入设备分类ID" />
          </el-form-item> -->
        <el-form-item label="购买日期" prop="buyDate">
          <el-date-picker
            clearable
            v-model="form.buyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listManager,
  getManager,
  delManager,
  addManager,
  updateManager,
} from "@/api/system/manager";

export default {
  name: "Manager",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      managerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 购买日期时间范围
      daterangeExamDate: [],
      // 购买日期时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineId: null,
        deviceName: null,
        deviceType: null,
        model: null,
        manufacturers: null,
        location: null,
        argument: null,
        deviceImage: null,
        principal: null,
        examDate: null,
        nextexamDate: null,
        dailyExam: null,
        useBranch: null,
        createTime: null,
        minedeviceId: null,
        cateId: null,
        buyDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        typeId: [
          { required: true, message: "资料类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeExamDate && "" != this.daterangeExamDate) {
        this.queryParams.params["beginExamDate"] = this.daterangeExamDate[0];
        this.queryParams.params["endExamDate"] = this.daterangeExamDate[1];
      }
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listManager(this.queryParams).then((response) => {
        this.managerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mineId: null,
        deviceName: null,
        deviceType: null,
        model: null,
        manufacturers: null,
        location: null,
        argument: null,
        deviceImage: null,
        principal: null,
        examDate: null,
        nextexamDate: null,
        dailyExam: null,
        useBranch: null,
        remark: null,
        createTime: null,
        updateTime: null,
        minedeviceId: null,
        cateId: null,
        buyDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeExamDate = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getManager(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateManager(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManager(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delManager(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/manager/export",
        {
          ...this.queryParams,
        },
        `manager_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  